<?php
namespace app\cms\model;
/* *
 * 栏目管理
 * */
class channel extends \app\base\model\AdminModel{
    protected $table ='cms_class';  //模型表

    //判断分类模型
    public function _model($fid){
        $fid = intval($fid);
        $rel = $this->where(array('id'=>$fid))->field('mid')->find();
        return model('module')->tables($rel['mid']);
    }

    //左链接列表
    public function _select($pid,$website_id){
        if($website_id){
            $condition['website_id'] = $website_id;
            $condition['pid'] = $pid;
        }else{
            $condition['pid'] = $pid;
        }
        return $this->table('cms_class as A')
              ->join('{pre}cms_module as B ON A.mid = B.id')
              ->join('{pre}website as C ON A.website_id = C.id')
              ->field('A.*,B.name as model_name,C.title as website_name')
              ->where($condition)
              ->limit($limit)
              ->order('a.neworder asc,a.id desc')
              ->select();
    }

    //列表
    public function _select_list($where,$order = NULL,$field = NULL){
       return $this->where($where)->field($field)->order($order)->select();
    }

    //单个
    public function _find($condition,$field = NULL){
        return $this->where($condition)->field($field)->find();
    }

    //单个+扩展查询
    public function _finds($uid,$field = NULL){
        $rel =  $this->where(array('id'=>$uid))->field($field)->find();
        if($rel['exts_mid']){
            $tables = model('module')->tables($rel['exts_mid']);
            $lists_ext = $this->table($tables)->where(array('channel_id'=>$uid))->find();
            if($lists_ext){
                $lists_ext = filter_string($lists_ext,array('channel_id'),1);
                $rel = array_merge($rel,$lists_ext);
            }
        }
        if(empty($rel['url'])){
          $rel['url'] = url('cms/default/lists',array('uid'=>$rel['id']));
        }
        return $rel;
    }
    
    //删除
    public function _delete($fid){
        //查看是否有子分类
        $subclass = $this->where(array('pid'=>$fid))->find(); 
        if($subclass){
            return '请先删除下级栏目';
        }
        //查看是否包含内容
        $tables = $this->_model($fid);
        $rel = $this->table($tables)->where(array('channel_id'=>$fid))->field('channel_id')->find();
        if($rel){
            return '请先删除栏目中的内容';
        }
        //查看扩展模型
        $info = $this->where(array('id'=>$fid))->field('id,exts_mid')->find();
        if($info['exts_mid']){
            $exts_tables = model('module')->tables($info['exts_mid']);
            $extrel = $this->table($exts_tables)->where(array('channel_id'=>$info['id']))->delete();
            if(!$extrel){
                return '删除扩展模型失败';
            }
        }
        $result = $this->where(array('id'=>$fid))->delete();
        if($result){
            return 0;
        }else{
            return '删除失败';
        }
    }  

    //编辑
    public function _update($data,$condition = NULL){
        if($condition){
            return $this->where($condition)->data($data)->update();
        }else{
            return $this->data($data)->insert();
        }
    } 

    //当前路径
    public function path($pid = 0,$url = NULL) {
        $condition['id'] = intval($pid);
         $result = $this->where($condition)->field('id,pid,name')->find();
        if ($result) {
            $Str = $this->path($result['pid'],$url);
            if($url){
                $Str .= ' / <a href="' . url(''.$url.'',array(id=>$result['id'])).'">' . $result['name'] . '</a>';
            }else{
                $Str .= ' / <a href="###">' . $result['name'] . '</a>';
            }
        }
        return $Str;
    }

    //编辑分类读取扩展内容
    public function editClassExt($cid,$mid){
        $mid = intval($mid);
        $table = model('module')->tables($mid);
        return  $this->table($table)->where(array('channel_id' => $cid))->find();
    }

    //编辑网站分类
    public function editClass($fid,$mid = null,$input_value = array()){
        $data = array();
        $data['type'] = $input_value['type'];
        $data['name'] = $input_value['name'];
        $data['url'] = $input_value['url'];
        $data['locks'] = $input_value['locks'];
        $data['tpl_list'] = $input_value['tpl_list'];
        $data['tpl_content'] = $input_value['tpl_content'];
        $data['tpl_plus'] = $input_value['tpl_plus'];
        $data['uptime'] = $input_value['uptime'];
        $info = $this->_update($data,array('id' =>$fid));
        if($info && $fid && $mid){
            $fieids = model('field')->_select(array('mid'=>$mid),'id desc','fields');
            if($fieids){
                foreach ($fieids as $key => $value) {
                    $ext[$value['fields']] = $input_value[$value['fields']]; //主表
                } 
            }
            //入库
            $tables = model('module')->tables($mid);
            $rel = $this->table($tables)->where(array('channel_id'=>$fid))->find();
            $ext['channel_id'] = $fid;
            if($rel){
                $this->table($tables)->data($ext)->where(array('channel_id'=>$fid))->update();
            }else{
                $this->table($tables)->data($ext)->insert();
            }
        }
        if($info){
            return TRUE;
        }else{
            return FALSE;
        }
    }

   //栏目下拉菜单
   public function ClassTree($rootid,$url,$website_id,$topid = TRUE){
        if($website_id){
            $sql = ' and website_id = '.$website_id;
        }
        $rootid = intval($rootid);
        $rel = $this->_select_list(array(0 => 'type <=1 and url=\'\' and pid = '.$rootid.$sql),'id,name,type');
        if($rel){
             if(!$topid)echo "<ul>";
             foreach($rel as $rs){
                echo '<li><a href="'.url($url,array('id' => $rs['id'])).'" class="p5">'.$rs['name']."</a>";
                $this->ClassTree($rs['id'],$url,$website_id,FALSE);
                echo '</li>';
             }
             if(!$topid)echo "</ul>";
        }
   }

   //栏目下拉菜单
   public function CreateClassTree($rootid,$cid = null,$topid = null){
        $rootid = intval($rootid);
        if(!$topid){
          echo '<li>';
            $rootclass = $this->_find(array(0 => 'type <=1 and url=\'\' and id = '.$rootid),'id,name,type');
            if($rootclass['type']){
               $rootdisabled = ' disabled="disabled"'; 
            }else{
              if($cid == $rootclass['id']){
                $rootradioSelected =' checked="checked"';
              }
            }
            echo '<input class="radio"'.$rootdisabled.$rootradioSelected.' type="radio" name="channel_id" value="'.$rootclass['id'].'" /> '.$rootclass['name'];
        }
        //包含所有自栏目
        $rel = $this->_select_list(array(0 =>'type <=1 and url=\'\' and pid = '.$rootid),'id,name,type');
        if($rel){
             echo "<ul>";
             foreach($rel as $rs){
                if($rs['type']){
                    $disabled = ' disabled="disabled"';
                }else{
                  if($cid == $rs['id']){
                      $radioSelected =' checked="checked"';
                  }
                }
                echo '<li><input class="radio"'.$disabled.$radioSelected.' type="radio" name="channel_id" value="'.$rs['id'].'" /> '.$rs['name'];
                unset($disabled);unset($radioSelected);
                $this->CreateClassTree($rs['id'],$cid,1);
                echo '</li>';
             }
             echo "</ul>";
        }
        if(!$topid)echo "</li>";
   }
   
   //递归搜当前目录下所有的下级目录ID（无限层型目录）
   public function AllChildClass($pid,$website_id = NULL){
       $data = $this->getAllClass($pid,$website_id);
       $str = $pid;
       if($data){
           foreach($data as $rs){
               $str.= ','.$rs['id'];
           }
       }
       return $str;
   }

   //获取指定ID下全部目录信息
   public function getAllClass($fid,$website_id = NULL){
        if($website_id){
            $condition['pid'] = $fid;
            $condition['website_id'] = $website_id;
        }else{
            $condition['pid'] = $fid;
        }
       $rel = $this->where($condition)->field('id,pid,name,type,uptime')->select();
        if($rel){
            foreach($rel as $rs){
                $str = $this->getAllClass($rs['id']);
                if($str){
                    foreach($str as $vo){
                       $rel[] = $vo;
                    }
                }
            }
        }
        return $rel;
   }

   //前台标签
   public function Label($labes,$ClassID){
        //是否有自定义SQL
        if(!empty($labes['es_where']))$where = ' and '.$labes['es_where'];
        //自定义排序
        $order = empty($labes['es_order'])?'id DESC':$labes['es_order'];
        //拼凑语句
        $sql = array(0 => 'locks = 1 and pid ='.$ClassID.$where);
        $result = $this->where($sql)->order($order)->field('id,pid,rootid,website_id,name,exts_mid,uptime,url')->select();
        if($result){ 
            foreach ($result as $key => $value) {
                if(empty($value['url'])){
                  $value['url'] = url('cms/default/lists',array('uid'=>$value['id']));
                }
                if($value['exts_mid']){
                    $tables = model('module','cms')->tables($value['exts_mid']);
                    $rel = $this->table($tables)->where(array('channel_id'=>$value['id']))->find();
                    if($rel){
                      $str = filter_string($rel,array('channel_id'));
                      $ary[0][$key] = array_merge($value,$str);
                    }else{
                      $ary[0][$key] = $value;
                    }
                }else{
                    $ary[0][$key] = $value;
                }
            }
            return $ary;
        }else{
            return array();
        }
   }
}